package com.koron.css2.clientCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserGroupBean;
import com.koron.css2.clientCenter.mapper.UserGroupMapper;
import com.koron.css2.clientCenter.queryBean.userGroupQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class UserGroupList implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		try {
			userGroupQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), userGroupQueryBean.class);
			UserGroupMapper mapper = factory.getMapper(UserGroupMapper.class);
			//分页设置
			PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
			List<UserGroupBean> list = mapper.selectList(queryBean);
			
			/*
			if(queryBean!=null && queryBean.getRemote()!=1) {
				//查询水司对应的行政区域
				DistrictMapper dmapper = factory.getMapper(DistrictMapper.class);
				DistrictBean selectBean = new DistrictBean();
				TWaterCompanyMapper twmapper = factory.getMapper(TWaterCompanyMapper.class,"_default");
				//获取水司设置的code
				String adminAreaDivi = twmapper.selectCompanyByCode(userInfo.getCurWaterCode());
				selectBean.setStatus(1);//只查询启用的
//				selectBean.setIsLeaf(0);//只查询目录
				selectBean.setCode(adminAreaDivi==null?"all":adminAreaDivi);
				List<DistrictBean> distList = dmapper.findDistrict(selectBean);

				for (UserGroupBean ubean : list) {
					for (DistrictBean bbean : distList) {
						if(bbean.getId().equals(ubean.getAdminArea())) {
							ubean.setAdminAreaName(bbean.getName());
							break;
						}
					}
				}
			}*/
			
			@SuppressWarnings("rawtypes")
			MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
			info.setData(new PageInfo<>(list));
			return info;
		}catch (Exception e) {
			logger.error("用户组列表查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
